﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class MaterialList : AuthBasePage
    {
        int m_count = 0;
        public int IPageIndex
        {
            get
            {
                if (null == Session["MaterialIPageIndex"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["MaterialIPageIndex"];
                }
            }
            set
            {
                Session["MaterialIPageIndex"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDDLMaterialStatus();
                BindDDLMatType();
                BindData(0);
            }
        }

        private void BindDDLMatType()
        {
            DataTable dt = MdMaterialtype.GetMdMaterialtypeAll().Tables[0];
            if (dt.Rows.Count > 0)
            {
                this.ddlMatTypeID.DataSource = dt;
                this.ddlMatTypeID.DataTextField = "MaterialTypeName";
                this.ddlMatTypeID.DataValueField = "MaterialTypeID";
                this.ddlMatTypeID.DataBind();
                UIHelper.InsertFirstItem(ddlMatTypeID);
            }
        }
        private void BindDDLMaterialStatus()
        {
            BindParameterForListControl("MaterialStatus", ddlMaterialStatus);
            UIHelper.InsertFirstItem(ddlMaterialStatus);
        }

        private void BindData(int pageIndex)
        {
            pageIndex = IPageIndex;
            DataTable dt = MdMaterial.SelectList(this.txtMaterialID.Text.Trim(), this.txtMaterialName.Text.Trim(), this.ddlMatTypeID.SelectedValue.Trim(), this.ddlMaterialStatus.SelectedValue.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
            
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
                //rb.Attributes.Add("onclick", "judge(this)");
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
            }
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("MaterialView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    Response.Redirect(string.Format("MaterialEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                   // MdMaterialsupplier.Del(strCode);
                    (new MdMaterial()).Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                default:
                    break;
            }
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            IPageIndex = e.PageIndex;
            BindData(IPageIndex);
        }

        //protected void btnExChange_Click(object sender, EventArgs e)
        //{
        //    string sReturn = "";
        //    foreach (GridViewRow gvr in grdResult.Rows)
        //    {
        //        Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;

        //        if (gvr.RowType == DataControlRowType.DataRow)
        //        {
        //            if (((RadioButton)gvr.FindControl("rbSelect")).Checked)
        //            {
        //                sReturn = HttpUtility.UrlEncode(lblMaterialID.Text, Encoding.UTF8);
        //                break;
        //            }
        //        }
        //    }
        //    if (!string.IsNullOrEmpty(sReturn))
        //    {
        //        MdMaterialEntity en=MdMaterial.GetMdMaterialByID(sReturn);
        //        if (en.Status.Trim() == "1")
        //        {
        //            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>FunAddCompany(" + sReturn + ");</script>");
        //        }
        //        else
        //        {
        //            UIHelper.AlertMessage(this.Page, "不能选择已停用的原料");
        //        }
        //    }
        //    else
        //    {
        //        UIHelper.AlertMessage(this.Page, "选择为空");
        //    }
        //}

        protected void btnHidden_Click(object sender, EventArgs e)
        {
            BindData(0);
        }
    }
}